from technology.models import Technology, Use
from project.models import Project
from pms.common.utils import flatten_view


def project_add_technology(project, technology, annotation=''):
    assert isinstance(project, Project)
    assert isinstance(technology, Technology)
    new_use = Use(pid=project, tid=technology, annotation=annotation)
    new_use.save()
    return new_use.pid, new_use.tid


def project_update_technology(project, technology, view):
    assert isinstance(project, Project)
    assert isinstance(technology, Technology)
    use = Use.objects.get(pid=project, tid=technology)
    argv = flatten_view(view, {
        'annotation': 'annotation'
    })
    use.__dict__.update(**argv)
    use.save()
    return use.pid, use.tid


def project_delete_technology(project, technology):
    assert isinstance(project, Project)
    assert isinstance(technology, Technology)
    use = Use.objects.get(pid=project, tid=technology)
    use.delete()
    return True
